{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 用Tkinter实现Python GUI"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Hello World"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tkinter import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "root = Tk()\n",
    "\n",
    "myLabel = Label(root, text='Hello world!')\n",
    "myLabel.grid()\n",
    "\n",
    "root.mainloop()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 用于构建管理界面的Tkinter小部件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from numpy import *\n",
    "from MyRegTree import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib\n",
    "matplotlib.use('TkAgg')\n",
    "from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg\n",
    "from matplotlib.figure import Figure"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def reDraw(tolS,tolN):\n",
    "    reDraw.f.clf()        # clear the figure\n",
    "    reDraw.a = reDraw.f.add_subplot(111)\n",
    "    if chkBtnVar.get():\n",
    "        if tolN < 2: tolN = 2\n",
    "        myTree=createTree(reDraw.rawDat, modelLeaf,modelErr, (tolS,tolN))\n",
    "        yHat = createForeCast(myTree, reDraw.testDat, modelTreeEval)\n",
    "    else:\n",
    "        myTree=createTree(reDraw.rawDat, ops=(tolS,tolN))\n",
    "        yHat = createForeCast(myTree, reDraw.testDat)\n",
    "    reDraw.a.scatter(reDraw.rawDat[:,0].A, reDraw.rawDat[:,1].A, s=5) #use scatter for data set\n",
    "    reDraw.a.plot(reDraw.testDat, yHat, linewidth=2.0) #use plot for yHat\n",
    "    reDraw.canvas.draw()\n",
    "    \n",
    "def getInputs():\n",
    "    try: tolN = int(tolNentry.get())\n",
    "    except: \n",
    "        tolN = 10 \n",
    "        print(\"enter Integer for tolN\")\n",
    "        tolNentry.delete(0, END)\n",
    "        tolNentry.insert(0,'10')\n",
    "    try: tolS = float(tolSentry.get())\n",
    "    except: \n",
    "        tolS = 1.0 \n",
    "        print(\"enter Float for tolS\")\n",
    "        tolSentry.delete(0, END)\n",
    "        tolSentry.insert(0,'1.0')\n",
    "    return tolN,tolS\n",
    " \n",
    "def drawNewTree():\n",
    "    tolN,tolS = getInputs()#get values from Entry boxes\n",
    "    reDraw(tolS,tolN)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "root = Tk()\n",
    "#用画布来替换绘制占位符，并删掉对应标签，即下面的Plot Place Holder那句\n",
    "reDraw.f = Figure(figsize = (5,4),dpi = 100)\n",
    "reDraw.canvas = FigureCanvasTkAgg(reDraw.f, master=root)\n",
    "reDraw.canvas.draw()\n",
    "reDraw.canvas.get_tk_widget().grid(row = 0,columnspan = 3)\n",
    "#待删除，只是代替图片占一个位置\n",
    "#Label(root,text='Plot Place Holder').grid(row = 0, columnspan = 3)\n",
    "#tolN\n",
    "Label(root,text = 'tolN').grid(row = 1, column = 0)\n",
    "tolNentry = Entry(root)  #Entry：单行文本输入框\n",
    "tolNentry.grid(row=1, column = 1)\n",
    "tolNentry.insert(0,'10')     #默认值为10\n",
    "#tolS\n",
    "Label(root,text = 'tolS').grid(row = 2, column = 0)\n",
    "tolSentry = Entry(root)\n",
    "tolSentry.grid(row=2, column = 1)\n",
    "tolSentry.insert(0,'1.0')     #默认值为1.0\n",
    "#按钮\n",
    "Button(root,text = 'ReDraw',command = drawNewTree).grid(row = 1, column = 2,rowspan = 3)\n",
    "#按钮整数值\n",
    "chkBtnVar = IntVar()\n",
    "#复选按钮\n",
    "chkBtn = Checkbutton(root,text = 'Model Tree',variable = chkBtnVar)\n",
    "chkBtn.grid(row = 3, column = 0,columnspan = 2)\n",
    "#初始化数据\n",
    "reDraw.rawDat = mat(loadDataSet('sine.txt'))\n",
    "reDraw.testDat = arange(min(reDraw.rawDat[:,0]),max(reDraw.rawDat[:,0]),0.01)\n",
    "reDraw(1.0,10)\n",
    "root.mainloop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
